﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;

using CharityGamesOnline.Modules.Common;
using CharityGamesOnline.Modules.Event;

namespace CharityGamesOnline.Modules.Game
{
    public partial class GameEnrollmentList : CGOBase
    {
        private int accountUserID;

        public int AccountUserID
        {
            get
            {
                if (accountUserID == 0)
                {
                    accountUserID = UserId;
                }
                return accountUserID;
            }
            set
            {
                accountUserID = value;
            }
        }

        public int EventDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["EventDetailTabID"]))
                {
                    return Int32.Parse(Settings["EventDetailTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        public int PayTabID
        {
            get
            {
                if (!Null.IsNull(Settings["PayTabID"]))
                {
                    return Int32.Parse(Settings["PayTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        public int GameDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["GameDetailTabID"]))
                {
                    return Int32.Parse(Settings["GameDetailTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        public bool ShowSectionHead
        {
            get
            {
                if (!Null.IsNull(Settings["ShowSectionHead"]))
                {
                    return Boolean.Parse(Settings["ShowSectionHead"].ToString());
                }
                else
                    return false; 
            }
        }

        public bool ShowModuleName
        {
            get
            {
                if (!Null.IsNull(Settings["ShowModuleName"]))
                {
                    return Boolean.Parse(Settings["ShowModuleName"].ToString());
                }
                else
                    return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {                    
                    dshSectionHead.Visible = ShowSectionHead;
                    BindData(true);
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void grdEnrollment_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbRank = e.Item.FindControl("lbRank") as Label;
                Label lbScore = e.Item.FindControl("lbScore") as Label;
                Label lbEndTime = e.Item.FindControl("lbEndTime") as Label;
                Label lbEnrollStatus = e.Item.FindControl("lbEnrollStatus") as Label;
                HyperLink lnkEvent = e.Item.FindControl("lnkEvent") as HyperLink;
                HyperLink lnkAction = e.Item.FindControl("lnkAction") as HyperLink;
                
                int enrollmentID = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "EnrollmentID").ToString());
                int eventID = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "EventID").ToString());
                int enrollStatusID = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "EnrollStatusID").ToString());
                decimal score = Decimal.Parse(DataBinder.Eval(e.Item.DataItem, "Score").ToString());
                DateTime endTime = DateTime.Parse(DataBinder.Eval(e.Item.DataItem, "EndTime").ToString());
                GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
                int userRank =ctrlEnroll.GetUserRank(enrollmentID, UserId);
                if (Null.IsNull(endTime))
                {
                    lbEndTime.Text = Localization.GetString("noRankReplacement", LocalResourceFile);
                }
                else
                {
                    lbEndTime.Text = endTime.ToString();
                }
                if(userRank==0)
                {
                    lbRank.Text = Localization.GetString("noRankReplacement", LocalResourceFile);
                }
                else
                {
                    lbRank.Text = userRank.ToString();
                }

                if (score == 0)
                {
                    lbScore.Text = Localization.GetString("noRankReplacement", LocalResourceFile);
                }
                else
                {
                    lbScore.Text = score.ToString();
                }

                EventController ctrlEvent = new EventController();
                EventInfo objEvent = ctrlEvent.GetEvent(PortalId, eventID);
                if (enrollStatusID!= (int)GameEnrollmentInfo.STATUS.COMPLETED && objEvent != null && DateTime.Now > objEvent.DateEnd)
                {
                    lbEnrollStatus.Text = Localization.GetString("expiredEnrollStatus", LocalResourceFile);
                    lnkAction.Visible = false;
                }
                else
                {
                    lbEnrollStatus.Text = DataBinder.Eval(e.Item.DataItem, "EnrollStatus").ToString();
                    
                    switch (enrollStatusID)
                    {
                        case (int)GameEnrollmentInfo.STATUS.ENROLLED:
                            lnkAction.Text = Localization.GetString("lnkActionPay", LocalResourceFile);
                            lnkAction.NavigateUrl = Globals.NavigateURL(PayTabID, string.Empty, "GameEnrollmentID=" + enrollmentID.ToString());
                            break;

                        case (int)GameEnrollmentInfo.STATUS.PAID:
                            lnkAction.Text = Localization.GetString("lnkActionPlay", LocalResourceFile);
                            lnkAction.NavigateUrl = Globals.NavigateURL(GameDetailTabID, string.Empty, "EventID=" + eventID.ToString());
                            break;

                        case (int)GameEnrollmentInfo.STATUS.PLAYING:
                            lnkAction.Text = Localization.GetString("lnkActionContinue", LocalResourceFile);
                            lnkAction.NavigateUrl = Globals.NavigateURL(GameDetailTabID, string.Empty, "EventID=" + eventID.ToString());
                            break;

                        case (int)GameEnrollmentInfo.STATUS.COMPLETED:
                        default:
                            lnkAction.Visible = false;
                            break;
                    }
                }
            }
 
        }

        protected void grdEnrollment_ItemCreated(object sender, DataGridItemEventArgs e)
        {
 
        }

        protected void cgoPaging_PageChangeEvent(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool refreshPaging)
        {
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            List<GameEnrollmentInfo> lstEnroll;
            cgoPaging.Visible = true;
            grdEnrollment.Visible = true;
            lbNoResult.Visible = false;
            if (refreshPaging)
            {
                cgoPaging.CurrentPage = 1;
                cgoPaging.TotalCount = 0;
                cgoPaging.Visible = false;
            }
            //lstEnroll = ctrlEnroll.GetEnrollments(PortalId, 0, 0, UserId, 0, refreshPaging, cgoPaging.PageSize, cgoPaging.CurrentPage);
            lstEnroll = ctrlEnroll.GetPlayedEvent(PortalId, UserId, refreshPaging, cgoPaging.PageSize, cgoPaging.CurrentPage);
            if (refreshPaging && lstEnroll.Count > 0 && lstEnroll[0].TotalRowCount > cgoPaging.PageSize)
            {
                cgoPaging.TotalCount = lstEnroll[0].TotalRowCount;
                cgoPaging.Visible = true;                
            }

            grdEnrollment.DataSource = lstEnroll;
            grdEnrollment.DataBind();

            if (lstEnroll.Count == 0)
            {
                grdEnrollment.Visible = false;
                lbNoResult.Visible = true;
            }            
        }
    }
}